<template>
  <el-card>
    <el-table :data="skuAttr" :border="true" style="margin: 10px 0px;">
      <el-table-column label="序号" width="80" type="index" align="center" />
      <el-table-column prop="skuName" label="名称" :show-overflow-tooltip="true" width="150" />
      <el-table-column prop="skuDesc" label="描述" width="150" />
      <el-table-column label="默认图片">
        <template #default="{ row }">
          <img :src="row.skuDefaultImg" style="width: 100px; height: 100px;" />
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量(g)" width="150" />
      <el-table-column prop="price" label="价格(元)" width="150" />
      <el-table-column label="操作" width="250" fixed="right">
        <template #default="{ row }">
          <el-button type="primary" size="small" :icon="row.isSale == 0 ? `Top` : `Bottom`"
            @click="handleSaleSku(row)"></el-button>
          <el-button type="primary" size="small" icon="Edit" @click="handleEdit(row)"></el-button>
          <el-button type="primary" size="small" icon="info-filled" @click="handlerSeeSku(row)"></el-button>
          <el-popconfirm class="box-item" :title="`确定删除${row.skuName}吗`" placement="top-start" width="200"
            @confirm="handleDelete(row)">
            <template #reference>
              <el-button type="danger" size="small" icon="Delete"></el-button>
            </template>
          </el-popconfirm>


        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-model:current-page="pageIndex" v-model:page-size="pageSize" :page-sizes="[2, 3, 5, 7]"
      @size-change="handleSizeChange" @current-change="handlerSkuList" :background="true"
      layout=" prev, pager, next, jumper , ->, sizes, total" :total="total" />
    <!-- 抽屉组件：展示产品的详情 -->
    <el-drawer v-model="drawer" direction="rtl">
      <template #header>
        <h4>看看商品详情</h4>
      </template>
      <template #default>
        <el-row style="margin: 10px 0px;">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px;">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px;">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px;">
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag v-for="(item, index) in skuInfo.skuAttrValueList" :key="index">{{ item.valueId }}</el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0px;">
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag v-for="(item, index) in skuInfo.skuSaleAttrValueList" :key="index">{{ item.saleAttrValueId
            }}</el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0px;">
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item">
                <img :src="item.imgUrl" style="width: 100%; height: 100%;" />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </el-card>


</template>

<script setup lang="ts">

import { reqCancelSale, reqOnSale, reqRemoveSkuById, reqSkuInfo, reqSkuList, } from '@/api/product/sku';
import type { SkuList, SkuVO } from '@/api/product/sku/type'
import { ElMessage } from 'element-plus';
import { ref, reactive, onMounted } from 'vue'


let skuAttr = ref<SkuList>([]);
let pageIndex = ref<number>(1);
let pageSize = ref<number>(5);
let total = ref<number>(0);
let drawer = ref<boolean>(false);

let skuInfo = ref<SkuVO>({
  id: 0,
  category3Id: 0,
  spuId: 0,
  tmId: 0,
  isSale: 0,
  skuName: '',
  price: 0,
  weight: 0,
  skuDesc: '',
  skuAttrValueList: [],
  skuSaleAttrValueList: [],
  skuImageList: [],
  skuDefaultImg: ''
})

//  查看商品详情的接口
const handlerSeeSku = async (row: SkuVO) => {
  console.log(row)
  drawer.value = true;
  let skuInfoResult = await reqSkuInfo(row.id as number);
  if (skuInfoResult.code === 200) {
    if (skuInfoResult.data) {
      skuInfo.value = skuInfoResult.data
    }
  } else {
    ElMessage.error(skuInfoResult.message);
  }
}
const handleSizeChange = (limit: number) => {
  console.log(`每页 ${limit} 条`)
  pageSize.value = limit;
  handlerSkuList();
}

// 处理sku 的上架情况
const handleSaleSku = async (row: SkuVO) => {
  //  如果是isSale===1 说明当前商品是上架的状态->更新为下架
  if (row.isSale === 0) {
    let onSaleResult = await reqOnSale(row.id as number);
    if (onSaleResult.code === 200) {
      ElMessage.success('下架成功');
      row.isSale = 1;
    } else {
      ElMessage.error('下架失败');
    }
  } else {
    let cancelResult = await reqCancelSale(row.id as number);
    if (cancelResult.code === 200) {

      ElMessage.success('上架成功');
      row.isSale = 0;
    } else {
      ElMessage.error('上架失败');
    }
  }
}
const handleDelete = async (row: SkuVO) => {
  console.log(row)
  let removeResult = await reqRemoveSkuById(row.id as number);
  if (removeResult.code != 200) {
    ElMessage.error(removeResult.message);
    return;
  }
  handlerSkuList(skuAttr.value.length > 1 ? pageIndex.value : pageIndex.value - 1);
}
const handleEdit = (row: SkuVO) => {
  console.log(row);
  ElMessage.success('程序员正在努力的更新中');
}


const handlerSkuList = async (page = 1) => {
  pageIndex.value = page;
  let skuListResult = await reqSkuList(pageIndex.value, pageSize.value);
  if (skuListResult.code != 200) {
    ElMessage.error(skuListResult.message);
    return;
  }
  let pvo = skuListResult.data;
  skuAttr.value = pvo?.list || []
  total.value = pvo?.total || 0
}

// 组件加载的时候调用一下
onMounted(() => {
  handlerSkuList()
})
</script>
<style scoped lang="scss">
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>